﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MyWindowsService
{
    public class MyConfig
    {
        private static MyConfig myInstance = new MyConfig();
        /// <summary>
        /// 对象唯一静态实例
        /// </summary>
        public static MyConfig Instance
        {
            get
            {
                return myInstance;
            }
        }

        /// <summary>
        /// 初始化对象
        /// </summary>
        private MyConfig()
        {
        }

        private bool bolLogRenamed = true;
        /// <summary>
        /// 是否记录重命名事件
        /// </summary>
        public bool LogRenamed
        {
            get { return bolLogRenamed; }
            set { bolLogRenamed = value; }
        }

        private bool bolLogChanged = true;
        /// <summary>
        /// 是否记录文件修改事件
        /// </summary>
        public bool LogChanged
        {
            get { return bolLogChanged; }
            set { bolLogChanged = value; }
        }
        private bool bolLogCreated = true;
        /// <summary>
        /// 是否记录对象创建事件
        /// </summary>
        public bool LogCreated
        {
            get { return bolLogCreated; }
            set { bolLogCreated = value; }
        }
        private bool bolLogDeleted = true;
        /// <summary>
        /// 是否记录对象删除事件
        /// </summary>
        public bool LogDeleted
        {
            get { return bolLogDeleted; }
            set { bolLogDeleted = value; }
        }

        private string[] myWatchedPaths = null;
        /// <summary>
        /// 监视的目录
        /// </summary>
        public string[] WatchedPaths
        {
            get { return myWatchedPaths; }
            set { myWatchedPaths = value; }
        }
        /// <summary>
        /// 读取系统配置
        /// </summary>
        public void Load()
        {
            myWatchedPaths = null;
            System.Collections.ArrayList paths = new System.Collections.ArrayList();
            using (System.Data.IDbCommand cmd = Util.DBConnection.CreateCommand())
            {
                cmd.CommandText = "Select ConfigName , ConfigValue From SystemConfig";
                System.Data.IDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    string Name = Convert.ToString(reader.GetValue(0));
                    if (Name == null)
                    {
                        continue;
                    }
                    Name = Name.Trim().ToLower();
                    string Value = Convert.ToString(reader.GetValue(1));
                    if (Name.StartsWith("path"))
                    {
                        paths.Add(Value.Trim());
                    }
                    else if (Name == "logrenamed")
                    {
                        bolLogRenamed = Convert.ToBoolean(Value);
                    }
                    else if (Name == "logchanged")
                    {
                        bolLogChanged = Convert.ToBoolean(Value);
                    }
                    else if (Name == "logdeleted")
                    {
                        bolLogDeleted = Convert.ToBoolean(Value);
                    }
                    else if (Name == "logcreated")
                    {
                        bolLogCreated = Convert.ToBoolean(Value);
                    }
                }
            }
            myWatchedPaths = (string[])paths.ToArray(typeof(string));
        }
        /// <summary>
        /// 保存系统配置
        /// </summary>
        public void Save()
        {
            using (System.Data.IDbCommand cmd = Util.DBConnection.CreateCommand())
            {
                cmd.CommandText = "Delete From SystemConfig";
                cmd.ExecuteNonQuery();
                cmd.CommandText = "Insert Into SystemConfig ( ConfigName , ConfigValue ) Values( ? , ? )";
                System.Data.IDbDataParameter pName = cmd.CreateParameter();
                cmd.Parameters.Add(pName);
                System.Data.IDbDataParameter pValue = cmd.CreateParameter();
                cmd.Parameters.Add(pValue);

                pName.Value = "LogRenamed";
                pValue.Value = bolLogRenamed.ToString();
                cmd.ExecuteNonQuery();

                pName.Value = "LogChanged";
                pValue.Value = bolLogChanged.ToString();
                cmd.ExecuteNonQuery();

                pName.Value = "LogDeleted";
                pValue.Value = bolLogDeleted.ToString();
                cmd.ExecuteNonQuery();

                pName.Value = "LogCreated";
                pValue.Value = bolLogCreated.ToString();
                cmd.ExecuteNonQuery();

                for (int iCount = 0; iCount < myWatchedPaths.Length; iCount++)
                {
                    string path = myWatchedPaths[iCount];
                    if (path == null || path.Trim().Length == 0)
                    {
                        continue;
                    }
                    pName.Value = "path" + iCount;
                    pValue.Value = path;
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }
}
